var NotePad= new Class({	
	 Implements: Log,	
	 options:{
	 	 "id":null,
	 	 "tilte":null,
	 	 "type":null,
	 	 "content":null,
	 	 "btime":null
	 	},
   initialize: function(){
		  this.options.id=null;
		  this.options.title="";
		  this.options.type=null;
		  this.options.content="";
		  this.options.btime=null;
		  
		  this.createWorker();		  
		  
     },
	 createWorker:function(workid){	 
		 this.mainWork=google.gears.factory.create('beta.workerpool');
	 },
	
	 dump:function(){

		 this.log('id='+this.options.id+'   title='+this.options.title+'   type='+this.options.type);
		 this.log("content="+this.options.content);		 
	 },	
	 notepadnew:function(notepadId){		 
		 if (notepadId==null){
			  this.options.id=null;
			  this.options.title=null;
			  this.options.type=null;
			  this.options.content="";
			  this.options.btime=null;
			  $('notetext').value="";
			  if ($defined(document.getElementById("NotepadTitle")))	{
			        $('NotepadTitle').value="";
			  }
			  if ($defined(document.getElementById("NotepadType")))	{
			        $('NotepadType').options[0].selected = true;    
			  }

			  
			  
		}else{
			  this.options.id=notepadId[0];
			  this.options.title=notepadId[1];
			  this.options.type=notepadId[2];
			  this.options.content=notepadId[3];
			  this.options.btime=null;
			  $('notetext').value=notepadId[3];
		}
	 }
	 
    
});



var notepad=new NotePad;
var saveWorkId;
	

function NotepadSaveVal(){	
	 // var element=document.getElementById("NotepadTitle")
	  //alert(typeof(element));
	  //var myElement = $(NotepadTitle);
	  //alert($defined(element)); 
	  //	alert(notepad.options.title);	 
	  //	alert($('notetext').value.trim());
 	  if ($('notetext').value.trim()!=""){
        /*if (($('notetext').value.trim()==notepad.options.content.trim()||){
        	  if (notepad.options.id!=null){
        	  	 return;
        	  }
        }
        */
        //alert($('notetext').value.trim());
        if ($defined(document.getElementById("NotepadTitle")))	{
        	//alert(notepad.options.title);	 
			     if ($('NotepadTitle').value.trim()!=""){
			     			notepad.options.title=$('NotepadTitle').value.trim(); 
			     }			     			     
			  }
			  if ($defined(document.getElementById("NotepadType"))){	
					 notepad.options.type=$('NotepadType').value; 
				}	 
					 
				//alert(notepad.options.title);	 
			  notepad.options.content=$('notetext').value; 
			  notepad.options.btime=new Date().format("db");
			  notepad.mainWork.sendMessage([{msgId:1},{id:notepad.options.id,title:notepad.options.title,type:notepad.options.type,content:notepad.options.content,btime:notepad.options.btime}], saveWorkId);
  	}
	  
 
}	
  	
var NotePadmainLoop=function(){

	notepad.mainWork.onmessage = function(a, b, message) {
		//alert(message.body[0].val)
		if (message.sender == saveWorkId) {
		  //alert(colaboxdb.testval);
			  if (message.body[0].msgId==0){
			  	  //alert("ok");
			  }else if (message.body[0].msgId==1){
			  	  //alert("s");
			  		notepad.options.id=message.body[0].ret;
			  		$('desktopFooter').innerText=message.body[0].val;
			  }	
		  //alert('Received message from worker ' + message.sender + ': \n ret:{' + message.body[0].ret+'\n value='+message.body[0].val+"}");
		}
				
	};
	//saveWorkId=notepad.mainWork.workerPool.createWorker("google.gears.workerPool.onmessage ="+String(saveWork));
	saveWorkId=notepad.mainWork.createWorkerFromUrl('scripts/notepad-worker.js');
	
	//notepad.mainWork.sendMessage([{msgid:0},{id:notepad.options.id,title:notepad.options.title,type:notepad.options.type,content:notepad.options.content,btime:notepad.options.btime}], saveWorkId);
	
	//init colaboxdb
	
	//msgid=0 init  1 save 

	notepad.mainWork.sendMessage([{msgId:0}], saveWorkId);
	
	//
	
	//var timer = google.gears.factory.create('beta.timer');
	//notepad.setVal();
  //timer.setInterval(NotepadSaveVal,10000);
  
  
	
	
}
NotePadmainLoop();


//notepad 界面调整
var logindiv=0;
var mySlide = new Fx.Slide('login').hide();  //starts the panel in closed state  
var DivAdvance=$('login0').innerHTML;
var DivSearch=$('login1').innerHTML;

$('login0').innerHTML="";
$('login1').innerHTML="";


var hideDiv=function(){
	  mySlide.toggle();
		logindiv=0;
}

var  bodyresize=function(){	

	hideDiv();
}





var searchMooTable=null;

var searchDivInit=function(){
	//alert($('pageWrapper').getStyle('width').toInt());
			searchMooTable = new MooDataTable('searchTable', {
					headers: [
						{id: "id", caption: "", sortable: false},
						{id: "title", caption: "Title", sortable: false},
						{id: "time", caption: "DateTime", sortable: false}
					],width:$('pageWrapper').getStyle('width').toInt()-20,perPage:5
				});
}

/*
$('notepadsearch').addEvent('click', function(e){

});
*/
var selectVal=function(val){
	  for (var i = 0; i < $('NotepadType').options.length; i++) {          
        if ($('NotepadType').options[i].value == val) {          
            $('NotepadType').options[i].selected = true;          
            break;          
        }         
     }   
}

$('notepadNew').addEvent('click', function(e){
	  notepad.notepadnew();
	  if (logindiv==1){
		e = new Event(e);
					mySlide.toggle();
					logindiv=0;
					e.stop();
	  } 
});

$('notepadSave').addEvent('click', function(e){
	  NotepadSaveVal();
});




$('toggleAdvance').addEvent('click', function(e){
	  if (logindiv==1){
		e = new Event(e);
					mySlide.toggle();
					logindiv=0;
					e.stop();
	  } else{
		  $('login').innerHTML=DivAdvance;//$('login0').innerHTML;
		  if (notepad.options.title==null){
		  		$('NotepadTitle').value="";
	   	}else{
	   		  $('NotepadTitle').value=notepad.options.title;
	   	}
	   	//$('NotepadType').selectedIndex=2;
	   	selectVal(notepad.options.type);
	   	//selectVal("live");
		  $('login').setStyle('height',60);
		  mySlide.hide();
					e = new Event(e);
					mySlide.toggle();
					logindiv=1;
					e.stop();	
     }
});
$('toggleSearch').addEvent('click', function(e){
	  if (logindiv==1){
		e = new Event(e);
					mySlide.toggle();
					logindiv=0;
					e.stop();
	  } else{

		 
		  $('login').innerHTML=DivSearch;//$('login1').innerHTML;
		  //if (searchMooTable==null) 
		  searchDivInit();
		  $('login').setStyle('height',$('notetext').getStyle('height'));
		  mySlide.hide();
					e = new Event(e);
					mySlide.toggle();
					logindiv=1;
					e.stop();		
	}
 });





